package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ZFBInterFace;
import com.xm.xmapp.request.bean.Add_pay_orderRequest;
import com.xm.xmapp.request.bean.Get_ali_pay_signRequest;
import com.xm.xmapp.request.bean.Get_weixin_pay_signRequest;
import com.xm.xmapp.request.bean.Zhima_AuthorizeRequest;
import com.xm.xmapp.response.bean.ResponseALIpaysign;
import com.xm.xmapp.response.bean.ResponseAddPayOrder;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.response.bean.ResponseWXpaysign;
import com.xm.xmapp.response.bean.ResponseZhimaAuthorize;
import com.xm.xmapp.tools.PayUitl;
import com.xm.xmapp.tools.StringTools;

public class WZYJActivity extends BaseActivity implements OnClickListener {

	private TextView pay;
	private RelativeLayout rel1, rel2;
	private LinearLayout lin1;
	private int flag;
	private double paymoney;
	private ImageView img1, img2;
	private TextView money, tishi, text1;
	private ResponseUserMoney bean;
	private LinearLayout zhima;
	private TextView go;
	boolean isAuthorize;

	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_WZYJ.DEFAULT:
			finish();
			break;

		default:
			break;
		}
		
		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wzyj);
	}

	@Override
	public void initView() {
		pay = (TextView) findViewById(R.id.pay);
		rel1 = (RelativeLayout) findViewById(R.id.rel1);
		rel2 = (RelativeLayout) findViewById(R.id.rel2);
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);
		lin1 = (LinearLayout) findViewById(R.id.lin1);
		money = (TextView) findViewById(R.id.money);
		tishi = (TextView) findViewById(R.id.tishi);
		text1 = (TextView) findViewById(R.id.text1);
		zhima = (LinearLayout) findViewById(R.id.zhima);
		go = (TextView) findViewById(R.id.go);
	}

	@Override
	public void initData() {
		flag = 1;
		setMyTitle("违章押金");
		bean = (ResponseUserMoney) getIntent().getSerializableExtra("bean");
		lin1.setVisibility(View.VISIBLE);
		java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
		money.setText(df.format(bean.car_cash_pledge - bean.frozen_money) + "元");
		tishi.setText("*违章押金可申请退还");
		text1.setText("违章押金不足,请先补足" + StringTools.getString(bean.car_cash_pledge) + "元押金");
		paymoney = bean.car_cash_pledge - bean.frozen_money;
		isAuthorize = getIntent().getBooleanExtra("isAuthorize", false);
		if (isAuthorize) {
			zhima.setVisibility(View.GONE);
		} else {
			zhima.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 获取支付宝支付参数
	 * 
	 * @param pay_order_number
	 */
	private void get_ali_pay_signRequest(String pay_order_number) {

		Get_ali_pay_signRequest requestBean = new Get_ali_pay_signRequest();
		requestBean.pay_order_number = pay_order_number;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(WZYJActivity.this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				pay.setClickable(true);
				promptDialog.dismiss();
				ResponseALIpaysign base = JSON.parseObject(response, ResponseALIpaysign.class);
				PayUitl.getInstence().zfb(WZYJActivity.this, new ZFBInterFace() {

					@Override
					public void success() {
						PayUitl.getInstence().afterPayMoney();
					}
				}, base.sign_string);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				pay.setClickable(true);
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void initEvent() {
		pay.setOnClickListener(this);
		rel1.setOnClickListener(this);
		rel2.setOnClickListener(this);
		go.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		//
		switch (v.getId()) {
		case R.id.go:
			promptDialog.show();
			Zhima_AuthorizeRequest requestBean = new Zhima_AuthorizeRequest();
			MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

				@Override
				public void onResponse(String response) {
					promptDialog.dismiss();
					ResponseZhimaAuthorize bean = JSON.parseObject(response, ResponseZhimaAuthorize.class);
					intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
					intent.putExtra("url", bean.authorize_url);
					intent.putExtra("title", "芝麻信用");
					startActivity(intent);
					finish();
				}
			}, new DisssmissInterFace() {
				@Override
				public void putError(VolleyError error) {
					promptDialog.dismiss();
				}
			});
			break;

		case R.id.pay:
//			if (paymoney < CodeConstant.MIN_FROZEN_PAY) {
//				Tools.showMessage("至少充值" + CodeConstant.MIN_FROZEN_PAY + "元");
//			} else {
				add_pay_orderRequest();
				pay.setClickable(false);
//			}

			break;
		case R.id.rel1:
			flag = 1;
			setImg();
			break;

		case R.id.rel2:
			flag = 2;
			setImg();
			break;

		default:
			break;
		}
	}

	private void setImg() {
		if (flag == 1) {
			img1.setImageResource(R.mipmap.icon_xuanze_nor);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		} else {
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_nor);
		}
	}

	private void add_pay_orderRequest() {

		Add_pay_orderRequest requestBean = new Add_pay_orderRequest();
		requestBean.type = 2;// 1=钱包充值，2=押金（后续参数再补）
		requestBean.price = paymoney;
		if (flag == 1) {
			requestBean.payType = 2;
		} else {
			requestBean.payType = 6;
		}

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				ResponseAddPayOrder base = JSON.parseObject(response, ResponseAddPayOrder.class);
				if (flag == 1)
					get_ali_pay_signRequest(base.pay_order_number);
				else
					get_weixin_pay_signRequest(base.pay_order_number);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				if (promptDialog != null)
					promptDialog.dismiss();
				pay.setClickable(true);
			}
		});

	}

	@Override
	public void onPause() {
		super.onPause();
		if (promptDialog != null) {
			promptDialog.dismiss();
		}
	}

	protected void get_weixin_pay_signRequest(String pay_order_number) {
		Get_weixin_pay_signRequest requestBean = new Get_weixin_pay_signRequest();
		requestBean.pay_order_number = pay_order_number;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				pay.setClickable(true);
				JSONObject obj = JSONObject.parseObject(response);
				ResponseWXpaysign base = JSON.parseObject(response, ResponseWXpaysign.class);
				base.package_str = obj.getString("package");
				PayUitl.getInstence().genWxPay(WZYJActivity.this, base, promptDialog);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				pay.setClickable(true);
				promptDialog.dismiss();
			}
		});
	}

}